<?php
/**
 * 周舟 hzboye010@163.com
 * addby sublime snippets
 * baby domain !-_-!
 */
namespace app\index\domain;

use app\index\domain\BaseDomain;
use app\system\api\BabyApi;
use app\system\api\MemberApi;
// use app\extend\Check;
// use app\shop\api\MemberConfigApi;

class BabyDomain extends BaseDomain{
  protected $business_code = 'baby';
  /**
   * 宝宝夭折
   */
  public function delete(){
		$this->checkVersion($this->api_ver);
		addLog("Baby/delete",$_GET,$_POST,"应用" . $this->client_id . "调用Baby单查接口");
		$uid = $this->_post("uid",0,"UID不能为空");
		$id  = $this->_post("id",0,"ID不能为空");
		// baby is exist ?
		$r   = apiCall(BabyApi::GET_INFO,[['id'=>$id,'uid'=>$uid]]);
		if(!$r['status']) $this->apiReturnErr($r['info']);
		if(empty($r['info']))  $this->apiReturnErr('该用户无此宝宝');
		$default = intval($r['info']['is_default']);
		$r = apiCall(BabyApi::DELETE,[['id'=>$id]]);
		if(!$r['status']) $this->apiReturnErr($r['info']);
		if(1===$default){
			$r = apiCall(BabyApi::GET_INFO,[['uid'=>$uid],'id desc']);
			if(!$r['status']) $this->apiReturnErr($r['info']);
			if(!empty($r['info'])){
				apiCall(BabyApi::SAVE,[['id'=>$r['info']['id']],['is_default'=>1]]);
			}
		}
		$this->apiReturnSuc('删除成功');
  }
  /**
   * 洗练宝宝
   */
  public function update(){
		$this->checkVersion($this->api_ver);
		addLog("Baby/update",$_GET,$_POST,"应用" . $this->client_id . "调用Baby单查接口");
		$uid = $this->_post("uid",0,"UID不能为空");
		$id  = $this->_post("id",0,"ID不能为空");
		$name 		  = $this->_post("name",'');
		$is_default = $this->_post("is_default",'');
		$sex 				= $this->_post("sex",'');
		$birthday 	= $this->_post("birthday",'');
		// $status 		= $this->_post("status",0);
		$head			  = $this->_post("head",'');
		// baby is exist ?
		$r   = apiCall(BabyApi::GET_INFO,[['id'=>$id,'uid'=>$uid]]);
		if(!$r['status']) $this->apiReturnErr($r['info']);
		if(empty($r['info']))  $this->apiReturnErr('该用户无此宝宝');
		$map = ['uid'=>$uid];
		if('' != trim($name))	$map['baby_name'] = trim($name);
		if('' != $is_default)	$map['is_default'] = intval($is_default);
		if('' != $sex)	$map['sex']=intval($sex);
		if('' != $birthday)	$map['birthday']=intval($birthday);
		if('' != $head)	$map['head']=intval($head);
		$this->check($map);
		$r = apiCall(BabyApi::SAVE,[['id'=>$id],$map]);
		if(!$r['status']) $this->apiReturnErr($r['info']);
		$this->apiReturnSuc('更新成功');
  }
  /**
   * 单个宝宝
   */
  public function detail(){
		$this->checkVersion($this->api_ver);
		addLog("Baby/detail",$_GET,$_POST,"应用" . $this->client_id . "调用Baby单查接口");
		$uid = $this->_post("uid",0,"UID不能为空");
		$id  = $this->_post("id",0,"ID不能为空");
		$r   = apiCall(BabyApi::GET_INFO,[['id'=>$id,'uid'=>$uid]]);
		if($r['status']) $this->apiReturnSuc($r['info']);
		else $this->apiReturnErr($r['info']);
  }
  /**
   * 我的宝宝 - 所有
   */
  public function query(){
		$this->checkVersion($this->api_ver);
		addLog("Baby/query",$_GET,$_POST,"应用" . $this->client_id . "调用Baby全查接口");
		$uid = $this->_post("uid",0,"UID不能为空");
		$r = apiCall(BabyApi::QUERY_NO_PAGING,[['uid'=>$uid]]);
		if($r['status']) $this->apiReturnSuc($r['info']);
		else $this->apiReturnErr($r['info']);
  }
  /**
   * 添加
   */
  public function add(){
		$this->checkVersion($this->api_ver);
		addLog("Baby/add",$_GET,$_POST,"应用" . $this->client_id . "调用Baby添加接口");

		$uid     		= $this->_post("uid",0,"UID不能为空");
		$name 		  = $this->_post("name",'','name不许为空');
		$is_default = $this->_post("is_default",0);
		$sex 				= $this->_post("sex",0);
		$birthday 	= $this->_post("birthday",'生日或预产期必须');
		// $status 		= $this->_post("status",0);
		$head			  = $this->_post("head",0);

		$map = [
			'uid'         =>intval($uid),
			'baby_name'   =>trim($name),
			'is_default'  =>intval($is_default),
			'sex'         =>intval($sex),
			'birthday'    =>intval($birthday),
			'baby_status' =>0,//intval($status),
			'head'        =>intval($head),
			'create_time' =>NOW_TIME,
		];
		$this->check($map);
		$result = apiCall(BabyApi::ADD,[$map]);
		if($result['status']){
			$id = intval($result['info']);
			$babyInfo = $this->getBabyInfo($id);
			$this->apiReturnSuc($babyInfo);
		}else{
			$this->apiReturnErr($result['info']);
		}
  }

  private function check($map){
  	if(isset($map['uid'])){
  		//user ID
  		$uid = $map['uid'];
  		if($uid<=0)	$this->apiReturnErr('uid 非法');
  		$result = apiCall(MemberApi::GET_INFO,[['uid'=>$uid]]);
  		if(!$result['status']) $this->apiReturnErr($result['info']);
  		if(empty($result['info'])) $this->apiReturnErr('uid 错误');
  	}
  	if(isset($map['baby_name'])){
			//name unique
			$result = apiCall(BabyApi::GET_INFO,[['baby_name'=>$map['baby_name']]]);
  		if(!$result['status']) $this->apiReturnErr($result['info']);
  		// if(!empty($result['info'])) $this->apiReturnErr('宝宝重名了');
  	}
		//sex 0:girl 1:boy
		if(isset($map['sex']) && !in_array($map['sex'],[0,1]))  $this->apiReturnErr('sex 非法');
		//birthday
		// if(isset($map['birthday'])intval($birthday))  $map['sex'] = $sex;
		// else $this->apiReturnErr('birthday 非法');
    //is_default 1:yes 0:no
		if(isset($map['is_default'])){
			if(!in_array($map['is_default'],[0,1])) $this->apiReturnErr('is_default 非法');
			//其余设置非默认
			if(1 === $map['is_default'] && $uid){
				$result = apiCall(BabyApi::SAVE,[['uid'=>$uid,'is_default'=>1],['is_default'=>0]]);
  			if(!$result['status']) $this->apiReturnErr($result['info']);
			}
		}
		//baby status 前端根据birthday判断
		//废弃 0:default 1:孕育中 2:备孕中 3:已出生
		// if(isset($map['baby_status']) && !in_array($map['baby_status'],[0,1,2,3])){}
  }

  private function getBabyInfo($id){
  	$result = apiCall(BabyApi::GET_INFO,[['id'=>$id]]);
  	$info = $result['info'];
  	if(!$result['status']) $this->apiReturnErr($info);
  	if(!empty($info)){
  		unset($info['baby_status']);
  		// $info['status'] = $this->getBabyStatus($info['birthday']);
  	}
  	return $info;
  }

  private function getBabyStatus($bir){
  	$len = intval($bir) - NOW_TIME;
  	if($len<0) return '已出生';
  	else if($len<3600*24*180) return '孕育中';
  	else return '备孕中';
  }
}